package com.xtx.dao;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.xtx.dataobject.PageInfo;
import com.xtx.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.sql.SQLException;
import java.util.List;

public abstract class BaseDAOImpl extends SqlMapClientDaoSupport{
	protected final Log logger = LogFactory.getLog(getClass());
	
	/**
	 * 用于分页查询,返回分页结果及总条数
	 * @param selStmName
	 * @param cntStmName
	 * @param query
	 * @return
	 */
	public List executeQueryForList(String selStmName, String cntStmName,
			PageInfo query){

		List result = null;

		if (StringUtil.isEmpty(selStmName) || StringUtil.isEmpty(cntStmName)
				|| (query == null)) {
			return null;
		}

		
			// 得到数据记录总数
			Integer countNumber = (Integer) getSqlMapClientTemplate()
					.queryForObject(cntStmName, query);

			if (countNumber != null) {
				// 设置当前查询的记录总数
				query.setTotalItem(countNumber.intValue());
				// 验证当前页数的合法性
				query.validatePageInfo();
				// 得到当前页的数据集
				result = getSqlMapClientTemplate().queryForList(selStmName,
						query);
			}
		

		return result;
	}
	
	/**
	 * 用于分页查询，返回分页结果及将当前结果条数set到query中
	 * @param selStmName
	 * @param query
	 * @return
	 */
	public <T> List<T> executeQueryForList(String selStmName,PageInfo query){
		List<T> result = null;
		if (StringUtil.isEmpty(selStmName)) {
			return null;
		}
		result = getSqlMapClientTemplate().queryForList(selStmName,query);
		if(result != null && query!= null){
			query.setReturnCount(result.size());
		}
		
		return result;
	}
	
	/**
	 * 返回查询列表
	 * 
	 * @param statementName
	 * @param parameterObject
	 * 
	 * @return
	 * @throws DataAccessException
	 */
	protected <T> List<T> queryForList(String statementName,Object parameterObject){
		List<T> resultList = this.getSqlMapClientTemplate().queryForList(statementName, parameterObject);
		return resultList;

	}
	
	/**
	 * 查询返回Object
	 * @param statementName
	 * @param parameterObject
	 * @return
	 */
	protected Object queryForObject(String statementName,Object parameterObject){
		Object returnObject = this.getSqlMapClientTemplate().queryForObject(
				statementName, parameterObject);
		return returnObject;
	}
	
	/**
	 * 更新数据库，可以插入一条记录，也可以删除一条记录 返回受影响的条数
	 * 
	 * @param statementName
	 * @param parameterObject
	 * @return 被更新的记录数
	 * @throws DataAccessException
	 */
	protected int executeUpdate(String statementName, Object parameterObject)
			throws DataAccessException {
		int updateRows = 0;
		updateRows = this.getSqlMapClientTemplate().update(statementName,
				parameterObject);
		return updateRows;
	}
	
	/**
	 * 插入一条记录
	 * 
	 * @param statementName
	 * @param parameterObject
	 * @return 新增加的记录主键
	 * @throws DataAccessException
	 */
	protected Object executeInsert(String statementName, Object parameterObject){
		Object back = null;

		back = this.getSqlMapClientTemplate().insert(statementName,
				parameterObject);

		return back;

	}
	
	/**
	 * 数据批操作，主要指批插入
	 * 
	 * @param stList
	 *            操作参数对象集合
	 * @return int 操作的记录数，操作失败时返回-1
	 */
	public Integer executeInsertBatch(final String sqlID, final List stList) {
		Integer result = new Integer(-1);
		
		if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
			result = (Integer) getSqlMapClientTemplate().execute(
					new SqlMapClientCallback() {
						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							Integer result = new Integer(-1);
							executor.startBatch();
							
							for (int i = 0; i < stList.size(); i++) {
								executor.insert(sqlID, stList.get(i));
							}
							
							result = new Integer(executor.executeBatch());
							
							return result;
						}
					});
		}

		return result;
	}
	
	/**
	 * 数据批操作，主要指批插入和批更新
	 * 
	 * @param stList
	 *            操作参数对象集合
	 * @return int 操作的记录数，操作失败时返回-1
	 */
	public Integer executeUpdateBatch(final String sqlID, final List stList){
		Integer result = new Integer(-1);

		if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
			result = (Integer) getSqlMapClientTemplate().execute(
					new SqlMapClientCallback() {
						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							Integer result = new Integer(-1);
							executor.startBatch();

							for (int i = 0; i < stList.size(); i++) {
								executor.update(sqlID, stList.get(i));
							}

							result = new Integer(executor.executeBatch());

							return result;
						}
					});
		}

		return result;
	}
	/**
	 * 数据批操作，批量删除
	 * 
	 * @param stList
	 *            操作参数对象集合
	 * @return int 操作的记录数，操作失败时返回-1
	 */
	public Integer executeDeleteBatch(final String sqlID, final List stList){
		Integer result = new Integer(-1);

		if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
			result = (Integer) getSqlMapClientTemplate().execute(
					new SqlMapClientCallback() {
						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							Integer result = new Integer(-1);
							executor.startBatch();

							for (int i = 0; i < stList.size(); i++) {
								executor.delete(sqlID, stList.get(i));
							}

							result = new Integer(executor.executeBatch());

							return result;
						}
					});
		}

		return result;
	}
}
